import axios from 'axios'
import qs from 'qs'
import vue from '../main'

// 超时码
const TIMEOUT_CODE = 404
const TIMEOUT_MSG = '请求超时'
const SUCCESS = 'success'

/**
 * @description: axios Get方式
 * @param {url, data, otherOptions}
 * @return:
 */
 export const ajaxGet = (url, data, otherOptions = {}) => {
    return new Promise((resolve) => {
      axios
        .get(url, {
          params: data,
          withCredentials: true,
          headers: {
            // token:
            //   router.name !== 'login' && getKey('token')
            //     ? getKey('token')
            //           : undefined,
              'Content-Type': "application/x-www-form-urlencoded;charset=UTF-8"
          },
          ...otherOptions,
        })
        .then((res) => {
          if (res.data.status === TIMEOUT_CODE) {
            vue.$warningMessage(TIMEOUT_MSG)
            return
          }
          if (res.data.status !== SUCCESS) {
            vue.$warningMessage(res.message)
          }

            resolve(res)
        })
          .catch((res) => {
              console.log('服务器出错，请联系开发人员')
              resolve(res)
          })
    })
}
/**
 * @description: axios Delete方式
 * @param {url, data, otherOptions}
 * @return:
 */
export const ajaxDelete = (url, data, otherOptions = {}) => {
    return new Promise((resolve) => {
        axios
            .delete(url, {
                params: data,
                withCredentials: true,
                headers: {
                    // token:
                    //   router.name !== 'login' && getKey('token')
                    //     ? getKey('token')
                    //           : undefined,
                    'Content-Type': "application/x-www-form-urlencoded;charset=UTF-8"
                },
                ...otherOptions,
            })
            .then((res) => {
                if (res.data.status === TIMEOUT_CODE) {
                    vue.$warningMessage(TIMEOUT_MSG)
                    return
                }
                if (res.data.status !== SUCCESS) {
                    vue.$warningMessage(res.message)
                }

                resolve(res)
            })
            .catch((res) => {
                console.log('服务器出错，请联系开发人员')
                resolve(res)
            })
    })
}

/**
 * @description: axios Post方式
 * @param {url,data}
 * @return:
 */
export const ajaxPost = (url, data, isJson = false) => {
    return new Promise((resolve) => {
        axios
            .post(url, isJson ? qs.parse(data) : qs.stringify(data), {
          // withCredentials: true,
          headers: {
            // token:
            //   router.name !== 'login' && getKey('token')
            //     ? getKey('token')
            //             : undefined,
              'Content-Type': isJson ? 'application/json' : 'application/x-www-form-urlencoded;charset=UTF-8',
          },
        })
        .then((res) => {
          if (res.data.status === TIMEOUT_CODE) {
            vue.$warningNotice(TIMEOUT_MSG)
            return
          }
          if (res.data.status !== SUCCESS) {
            vue.$warningNotice(res.data.message)
          }

            resolve(res)
        })
            .catch((res) => {
                console.log('服务器出错，请联系开发人员')
                resolve(res)
            })
    })
}
/**
 * @description: axios Put方式
 * @param {url,data}
 * @return:
 */
export const ajaxPut = (url, data, isJson = false) => {
    return new Promise((resolve) => {
        axios
            .put(url, isJson ? qs.parse(data) : qs.stringify(data), {
                // withCredentials: true,
                headers: {
                    // token:
                    //   router.name !== 'login' && getKey('token')
                    //     ? getKey('token')
                    //             : undefined,
                    'Content-Type': isJson ? 'application/json' : 'application/x-www-form-urlencoded;charset=UTF-8',
                },
            })
            .then((res) => {
                if (res.data.status === TIMEOUT_CODE) {
                    vue.$warningNotice(TIMEOUT_MSG)
                    return
                }
                if (res.data.status !== SUCCESS) {
                    vue.$warningNotice(res.data.message)
                }

                resolve(res)
            })
            .catch((res) => {
                console.log('服务器出错，请联系开发人员')
                resolve(res)
            })
    })
}
